package org.okkam.refine.strategy;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.strategy.AddressCandidate;
import org.okkam.refine.strategy.CandidateStrategy;

public class AddressCandidate implements CandidateStrategy {
        
        private static Log log = LogFactory.getLog(AddressCandidate.class);

        @Override
        public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {
                   
                   Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
                    Map<String, String> propValueNewEntity = new HashMap<String, String>();
                    boolean match = false;
                    
                    while (attributeTypeIterator.hasNext()) {
                        AttributeType attributeType = attributeTypeIterator.next();
                        String term = attributeType.getName().getLocalPart().toLowerCase();
                        String value = attributeType.getValue().toLowerCase().replace("\"", "");
                        AttributeMetadataType metadata = attributeType.getMetadata();
                        propValueNewEntity.put(term, value);
                }            
                    
                    Iterator entityIterator = entityList.iterator();
                    while (entityIterator.hasNext()) {
                            XMLEntityConverter converter = new XMLEntityConverter();
                            Entity entity = null;
                            try {
                                entity = converter.xmlToEntity( entityIterator.next().toString());
                            } catch (JAXBException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            ProfileType profile = entity.getProfile();
                            AttributesType attributesType = profile.getAttributes();
                            List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
                            Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

                            Map<String, String> propValueResponse = new HashMap<String, String>();

                            while (attributeTypeIteratorresponse.hasNext()) {
                                    AttributeType attributeType = attributeTypeIteratorresponse.next();
                                    String term = attributeType.getName().getLocalPart().toLowerCase();
                                    String value = attributeType.getValue().toLowerCase().replace("\"", "");
                                    propValueResponse.put(term, value);
                            }
                                
         //Based on toponomi,...          
            if ((propValueResponse.containsKey("comune")
                            && propValueResponse.containsKey("numero_civico")
                            && propValueResponse.containsKey("toponimo"))) {
                
                        if (propValueResponse.get("comune").equals(propValueNewEntity.get("comune"))
                               
                                && propValueResponse.get("numero_civico").equals(propValueNewEntity.get("numero_civico"))
                                && propValueResponse.get("toponimo").equals(propValueNewEntity.get("toponimo"))) {
                           
                            match = true;
                            
                            return match;
                        }
                    }
                    
                }
            return match;
        }
}